using System.Collections;
using UnityEngine;

public class RCCEnterExitCar : MonoBehaviour
{
	private GameObject carCamera;

	private GameObject player;

	private GameObject dashboard;

	public Transform getOutPosition;

	private bool opened;

	private float waitTime = 1f;

	private bool temp;

	private void Awake()
	{
		carCamera = UnityEngine.Object.FindObjectOfType<RCCCarCamera>().gameObject;
		carCamera.GetComponent<Camera>().enabled = false;
		carCamera.GetComponent<AudioListener>().enabled = false;
		GetComponent<RCCCarControllerV2>().runEngineAtAwake = false;
		GetComponent<RCCCarControllerV2>().canControl = false;
		GetComponent<RCCCarControllerV2>().engineRunning = false;
		if ((bool)carCamera.GetComponent<RCCCarCamera>())
		{
			carCamera.GetComponent<RCCCarCamera>().enabled = true;
		}
		if ((bool)UnityEngine.Object.FindObjectOfType<RCCDashboardInputs>())
		{
			dashboard = UnityEngine.Object.FindObjectOfType<RCCDashboardInputs>().gameObject;
		}
		if (!getOutPosition)
		{
			GameObject gameObject = new GameObject("Get Out Position");
			gameObject.transform.SetParent(base.transform);
			gameObject.transform.localPosition = new Vector3(-3f, 0f, 0f);
			gameObject.transform.localRotation = Quaternion.identity;
			getOutPosition = gameObject.transform;
		}
		if ((bool)GetComponent<RCCCarCameraConfig>())
		{
			GetComponent<RCCCarCameraConfig>().enabled = false;
		}
	}

	private void Start()
	{
		if ((bool)dashboard)
		{
			StartCoroutine("DisableDashboard");
		}
	}

	private IEnumerator DisableDashboard()
	{
		yield return new WaitForFixedUpdate();
		dashboard.SetActive(value: false);
	}

	private void Update()
	{
		if (UnityEngine.Input.GetKeyDown(KeyCode.E) && opened && !temp)
		{
			GetOut();
			opened = false;
			temp = false;
		}
	}

	private IEnumerator Act(GameObject fpsplayer)
	{
		player = fpsplayer;
		if (!opened && !temp)
		{
			GetIn();
			opened = true;
			temp = true;
			yield return new WaitForSeconds(waitTime);
			temp = false;
		}
	}

	private void GetIn()
	{
		if ((bool)carCamera.GetComponent<RCCCamManager>())
		{
			carCamera.GetComponent<RCCCamManager>().cameraChangeCount = 10;
			carCamera.GetComponent<RCCCamManager>().ChangeCamera();
		}
		carCamera.transform.GetComponent<RCCCarCamera>().playerCar = base.transform;
		player.transform.SetParent(base.transform);
		player.transform.localPosition = Vector3.zero;
		player.transform.localRotation = Quaternion.identity;
		player.SetActive(value: false);
		carCamera.GetComponent<Camera>().enabled = true;
		if ((bool)GetComponent<RCCCarCameraConfig>())
		{
			GetComponent<RCCCarCameraConfig>().enabled = true;
		}
		GetComponent<RCCCarControllerV2>().canControl = true;
		if ((bool)dashboard)
		{
			dashboard.SetActive(value: true);
		}
		carCamera.GetComponent<AudioListener>().enabled = true;
		SendMessage("StartEngine");
		Cursor.lockState = CursorLockMode.None;
	}

	private void GetOut()
	{
		player.transform.SetParent(null);
		player.transform.position = getOutPosition.position;
		player.transform.rotation = getOutPosition.rotation;
		player.SetActive(value: true);
		carCamera.GetComponent<Camera>().enabled = false;
		if ((bool)GetComponent<RCCCarCameraConfig>())
		{
			GetComponent<RCCCarCameraConfig>().enabled = false;
		}
		carCamera.GetComponent<AudioListener>().enabled = false;
		GetComponent<RCCCarControllerV2>().canControl = false;
		GetComponent<RCCCarControllerV2>().engineRunning = false;
		if ((bool)dashboard)
		{
			dashboard.SetActive(value: false);
		}
		Cursor.lockState = CursorLockMode.Locked;
	}
}
